import { getMember, getMemberInfo, postMember, putMember, postMember2, putMember2, selectMemberByAgentId } from '@/base/api/MemberApi';
import { Member } from '@/base/model/Member';
import { getAgentList } from '@/base/api/AgentApi';
import { getCompanyList } from '@/base/api/CompanyApi';
import { deepAssign } from '@/base/util/Objects';
import { Page } from '@/base/Page';
import { Company } from '@/base/model/Company';
import { Agent } from '@/base/model/Agent';
import { formatDate } from "@/base/util/Dates";
import { getLabelList, getLabelSort } from "@/base/api/LabelApi";

export class FormBloc {
  form = new Member() || '';
  loading = false;
  sortList: any = [];
  companyList: any = [];
  company: Page<Company> = new Page<Company>([], 0, 1, 10, 0, []);
  angent: Page<Agent> = new Page<Agent>([], 0, 1, 15, 0, []);
  careerList: any = []
  hobbyList: any = []
  career: any = []
  hobby: any = []

  load(id: number) {
    return getMemberInfo(id)
      .then((res) => {
        this.form = res.data;
        if (this.form.career) {
          this.career = this.form.career.split(',').map(str => parseInt(str));
        }
        if (this.form.hobby) {
          this.hobby = this.form.hobby.split(',').map(str => parseInt(str));
        }
        console.log('this.career', res)
      });
  }

  save(id: number | '') {
    // const entity: any = {
    //   password: this.form.password,
    //   phone: this.form.phone
    // };
    // return id ? putMember(id, entity) : postMember(entity);//修改手机号 密码
    if (id) {
      this.form.birthday ? this.form.birthday = formatDate(
        new Date(this.form.birthday),
        'yyyy-MM-dd') : ''
      const entity: any = {
        id: id,
        companyId: this.form.companyId,
        agentId: this.form.agentId,
        phone: this.form.phone,
        nickname: this.form.nickname,
        birthday: this.form.birthday || '',
        sex: this.form.sex,
        commentStatus:this.form.commentStatus,
        height: this.form.height,
        weight: this.form.weight,
        career: this.career.join(','),
        hobby: this.hobby.join(','),
      }
      return putMember2(entity)
    } else {
      this.form.birthday ? this.form.birthday = formatDate(
        new Date(this.form.birthday),
        'yyyy-MM-dd') : ''
      const entityPost: any = {
        companyId: this.form.companyId,
        agentId: this.form.agentId,
        phone: this.form.phone,
        nickname: this.form.nickname,
        birthday: this.form.birthday || '',
        sex: this.form.sex,
        commentStatus:this.form.commentStatus,
        height: this.form.height,
        weight: this.form.weight,
        career: this.career.join(','),
        hobby: this.hobby.join(','),
      }
      return postMember2(entityPost)
    }
  }

  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getAgentList({
      pageIndex,
      pageSize,
      companyId: this.form.companyId[0]
    }).then((res) => {
      this.sortList = res.data.items.map(function (item) {
        var obj = { id: 0, agentName: '' };
        // @ts-ignore
        obj.id = item.id;
        obj.agentName = item.agentName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }

  getCompanyList(pageIndex: number,
    pageSize: number,
    sortDirection: string) {
    return getCompanyList({ pageIndex, pageSize, sortDirection, name: '', createBegin: '' })
      .then((res) => {
        deepAssign(this.company, res.data)
        this.companyList = res.data.items.map(item => {
          let obj = { companyId: 0, companyName: '' }
          obj.companyId = item.id;
          obj.companyName = item.name
          return obj
        })
        return res;
      }).finally(() => {
        this.loading = false;
      });
  }

  getLabelList(id: any) {
    const companyId: any = [id]
    getLabelList({
      pageIndex: 1,
      pageSize: 50,
      // companyId: companyId,
      labelsortId: 1,
      sortDirection: 'DESC'
    }).then((res) => {
      this.hobbyList = res.data.items
    })
    getLabelList({
      pageIndex: 1,
      pageSize: 50,
      // companyId: companyId,
      labelsortId: 2,
      sortDirection: 'DESC'
    }).then((res) => {
      this.careerList = res.data.items
      console.log('this.careerList', this.careerList)
    })

  }
}
